package com.woyou.service.pay;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import com.alipay.sdk.app.PayTask;
import com.woyou.bean.AlipayResult;
import com.woyou.service.ThreadPoolManager_;
import com.woyou.service.pay.PayManger.IPayCallback;
import com.woyou.utils.payutils.SignUtils;

import android.app.Activity;

/**
 * @author blanks
 * @创建时间 2015年9月9日 下午4:14:05
 * @version v1.0 在线支付管理类
 */
final class AliPayImp {

	private Activity mActivity;

	private IPayCallback callback;
	private String price;
	private String commName;
	private String orderId;

	/**
	 * 
	 */
	public AliPayImp(Activity mActivity) {
		// TODO Auto-generated constructor stub
		this.mActivity = mActivity;
	}

	/**
	 * TODO 支付宝支付
	 * 
	 * @param callback
	 *            商品名称
	 * @param price
	 *            价格
	 * @param payCallback
	 *            支付回调接口
	 * @param orderId
	 *            商户订单号
	 */
	void alipay(final IPayCallback callback, String commName, String price, String orderId) {
		this.callback = callback;
		this.commName = commName;
		this.orderId = orderId;
		this.price = price;

		// 订单
		String orderInfo = getOrderInfo();
		String sign = sign(orderInfo);
		try {
			// 仅需对sign 做URL编码
			sign = URLEncoder.encode(sign, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			callback.onError(PayConfig.PAY_EXCEPTION);
		}
		// 完整的符合支付宝参数规范的订单信息
		final String payInfo = orderInfo + "&sign=\"" + sign + "\"&" + getSignType();

		ThreadPoolManager_.getInstance_(mActivity).executeTask(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				PayTask payTask = new PayTask(mActivity);
				String result = payTask.pay(payInfo);
				AlipayResult alipayResult = new AlipayResult(result);
				int error = Integer.parseInt(alipayResult.getResultStatus());
				if (error == 9000) {
					callback.onSucced();
				} else {
					callback.onError(error);
				}

			}
		});
	}

	/**
	 * sign the order info. 对订单信息进行签名
	 * 
	 * @param content
	 *            待签名订单信息
	 */
	String sign(String content) {
		return SignUtils.sign(content, PayConfig.ALIPAY_RSA_PRIVATE);
	}

	/**
	 * get the sign type we use. 获取签名方式
	 * 
	 */
	String getSignType() {
		return "sign_type=\"RSA\"";
	}

	/**
	 * create the order info. 创建订单信息
	 * 
	 */
	String getOrderInfo() {

		// 签约合作者身份ID
		String orderInfo = "partner=" + "\"" + PayConfig.ALIPAY_PARTNER + "\"";

		// 签约卖家支付宝账号
		orderInfo += "&seller_id=" + "\"" + PayConfig.ALIPAY_SELLER + "\"";

		// 商户网站唯一订单号
		orderInfo += "&out_trade_no=" + "\"" + orderId + "\"";

		// 商品名称
		orderInfo += "&subject=" + "\"" + "我有外卖订单：" + orderId + "\"";

		// 商品详情
		orderInfo += "&body=" + "\"" + commName + "下单，在线付款" + "\"";

		// 商品金额
		orderInfo += "&total_fee=" + "\"" + price + "\"";

		// 服务器异步通知页面路径
		orderInfo += "&notify_url=" + "\"" + PayConfig.ALIPAY_NOTIFY_URL + "\"";

		// 服务接口名称， 固定值
		orderInfo += "&service=\"mobile.securitypay.pay\"";

		// 支付类型， 固定值
		orderInfo += "&payment_type=\"1\"";

		// 参数编码， 固定值
		orderInfo += "&_input_charset=\"utf-8\"";

		orderInfo += "&it_b_pay=\"" + PayConfig.ALIPAY_TIMEOUT + "\"";

		return orderInfo;
	}
}
